import {post, token} from "@/common/index";
import axios from "axios";

/**
 * 获取后缀名
 * @param filename
 */
export const getFileSuffix = filename => {
    return filename.match(/\.([^.]+)$/)?.[1]
}

/**
 * 从文件数组里删除某个文件名
 * 数组格式为 a|b|c，separator是传入的分隔符
 */

export const deleteFilename = (separator, a, replace) => {
    const newArr = a.split(separator);
    const filteredArr = newArr.filter(str => str !== replace);
    return filteredArr.join(separator);
}

export const downloadWord = (url, data: any = {}) => {
    post(url, data, {
        responseType: 'blob',
    })
        .then(response => {
            // 创建一个下载链接
            const url = window.URL.createObjectURL(new Blob([response]));
            const link = document.createElement('a');
            link.href = url;
            // 设置下载链接的文件名
            link.setAttribute('download', 'filename.docx');
            // 触发点击事件以开始下载
            link.click();
        })
        .catch(error => {
            // 错误处理
            console.error(error);
        });
}